<template>
	<view class="container">
		<view v-if="addressList.length" class="address-list">
			<view class="address-item" v-for="(item,i) in addressList" :key="item.id" @click="emitAddressInfo(item)">
				<view class="item-top f-c-between">
					<view class="name">{{item.realname}}</view>
					<view class="phone">{{item.mobile}}</view>
				</view>
				<view class="item-detail">{{item.province}}{{item.city}}{{item.distinct}}{{item.address}}</view>
				<view class="item-default f-c-between">
					<view class="item-select" @click.stop="changeDefaultAddress(item)">
						<span v-if="item.default==1" class="iconfont icon-xuanzhong"></span>
						<span v-else class="iconfont icon-weixuanzhong"></span>
						默认地址
					</view>
					<view class="item-edit" @click.stop="goPage('/pages/address/create?id='+item.id)">编辑</view>
				</view>
			</view>

		</view>

		<view v-else class="address-empty">
			<view class="empty-label">暂无地址</view>
		</view>

		<view class="footer-fixed">
			<button class="btn-add" @click="goPage('/pages/address/create')">新增收货地址</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: []
			}
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			//获取地址列表
			getAddressList() {
				this.$api.get_address_list().then(res => {
					this.addressList = res || []
				})
			},

			//发射地址信息
			emitAddressInfo(item) {
				uni.$emit('addressInfo', item)
				uni.navigateBack()
			},

			//跳转页面
			goPage(url) {
				uni.navigateTo({
					url
				})
			},

			//切换默认地址
			changeDefaultAddress(item) {
				if (item.default == 1) {
					return
				}
				this.$api.save_address_edit({
					id: item.id,
					default: 1
				}).then(res => {
					this.getAddressList()
				})
			}
		}
	}
</script>

<style>
	page {
		background: #F4F7F7;
	}
</style>

<style lang="scss" scoped>
	.container {
		padding-bottom: 200rpx;
	}

	.address-list {
		padding-top: 24rpx;

		.address-item {
			padding: 26rpx 42rpx 0;
			background: #FFFFFF;
			box-shadow: 0px 0px 18rpx 0px rgba(0, 0, 0, 0.11);
			margin-bottom: 18rpx;

			.item-top {
				.name {
					font-weight: 500;
					font-size: 34rpx;
					color: #333333;
				}

				.phone {
					font-weight: 400;
					font-size: 32rpx;
					color: #666666;
				}
			}

			.item-detail {
				font-weight: 400;
				font-size: 32rpx;
				color: #666666;
				padding: 10rpx 0 50rpx;
				border-bottom: 1px solid #c8c4bd;
			}

			.item-default {
				padding: 20rpx 0;

				.item-select {
					font-weight: 400;
					font-size: 32rpx;
					color: #666666;

					.iconfont {
						font-size: 36rpx;
						color: #FFC046;
						margin-right: 12rpx;
					}

					.icon-weixuanzhong {
						color: #c9c9c9;
					}
				}

				.item-edit {
					font-weight: 400;
					font-size: 32rpx;
					color: #666666;
				}
			}
		}
	}


	.address-empty {
		padding: 0 32rpx;

		.empty-label {
			font-weight: 500;
			font-size: 32rpx;
			color: #9B9B9B;
			text-align: center;
			padding: 60% 0;
		}
	}


	.footer-fixed {
		padding: 30rpx 32rpx;
		background-color: #F4F7F7;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;

		.btn-add {
			height: 76rpx;
			line-height: 76rpx;
			background: #FFC046;
			border-radius: 35rpx;
			font-size: 32rpx;
			color: #FFFFFF;
		}
	}
</style>